package com.atung.merchant.service.impl;

import com.atung.merchant.config.auth.AuthUserMangerContext;
import com.atung.merchant.config.auth.PwdCreateUtil;
import com.atung.merchant.mapper.BusMerchantMapper;
import com.atung.merchant.mapper.BusMerchantUserMapper;
import com.atung.merchant.po.BusMerchant;
import com.atung.merchant.po.BusMerchantUser;
import com.atung.merchant.result.ApiResult;
import com.atung.merchant.service.MerchantService;
import com.atung.merchant.vo.IdRequest;
import com.atung.merchant.vo.ResponseMerchantInfo;
import com.atung.merchant.vo.ResponseMerchantUser;
import com.atung.merchant.vo.save.SaveMerchantRequest;
import com.atung.merchant.vo.save.SaveMerchantUserRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
@Service
public class MerchantServiceImpl implements MerchantService {


    @Resource
    private BusMerchantMapper busMerchantMapper;
    @Resource
    private BusMerchantUserMapper busMerchantUserMapper;

    @Override
    public ApiResult saveMerchantService(SaveMerchantRequest saveMerchantRequest) {
        return null;
    }

    @Override
    public ApiResult saveMerchantUserService(SaveMerchantUserRequest saveMerchantUserRequest) {


        if (Objects.isNull(saveMerchantUserRequest.getId())) {
            //新增
            if (checkUserPhone(saveMerchantUserRequest.getPhone())) {
                return ApiResult.fail("手机号已被注册");
            }
            BusMerchantUser busMerchantUser = new BusMerchantUser();
            BeanUtils.copyProperties(saveMerchantUserRequest, busMerchantUser);
            busMerchantUser.setType(saveMerchantUserRequest.getRole().ordinal());
            busMerchantUser.setMerchantId(AuthUserMangerContext.getMerchantId());
            busMerchantUser.setPassword(PwdCreateUtil.createPwd(saveMerchantUserRequest.getPassword()));
            busMerchantUserMapper.insert(busMerchantUser);
        } else {
            //修改
            BusMerchantUser busMerchantUser = busMerchantUserMapper.selectById(saveMerchantUserRequest.getId());
            if (Objects.isNull(busMerchantUser) || !busMerchantUser.getMerchantId().equals(AuthUserMangerContext.getMerchantId())) {
                return ApiResult.fail("数据不存在");
            }
            if (!saveMerchantUserRequest.getPhone().equals(busMerchantUser.getPhone())) {
                if (checkUserPhone(saveMerchantUserRequest.getPhone())) {
                    return ApiResult.fail("手机号已被注册");
                }

            }
            String pwd = saveMerchantUserRequest.getPassword();
            if (saveMerchantUserRequest.getPassword().equals("******")) {
                pwd = busMerchantUser.getPassword();
            } else {
                pwd = PwdCreateUtil.createPwd(saveMerchantUserRequest.getPassword());
            }
            BeanUtils.copyProperties(saveMerchantUserRequest, busMerchantUser);
            busMerchantUser.setType(saveMerchantUserRequest.getRole().ordinal());
            busMerchantUser.setPassword(pwd);
            busMerchantUserMapper.updateById(busMerchantUser);

        }
        return ApiResult.ok();
    }

    public Boolean checkUserPhone(String phone) {
        QueryWrapper<BusMerchantUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        queryWrapper.eq("merchant_id", AuthUserMangerContext.getMerchantId());
        Integer userCount = busMerchantUserMapper.selectCount(queryWrapper);

        return userCount > Integer.valueOf(0) ? true : false;


    }

    @Override
    public ResponseMerchantInfo queryMerchantInfoService() {
        Long merchantId = AuthUserMangerContext.getMerchantId();

        BusMerchant busMerchant = busMerchantMapper.selectById(merchantId);
        if (Objects.isNull(busMerchant)) {
            return null;
        }
        ResponseMerchantInfo responseMerchantInfo = new ResponseMerchantInfo();
        BeanUtils.copyProperties(busMerchant, responseMerchantInfo);
        return responseMerchantInfo;
    }

    @Override
    public List<ResponseMerchantUser> queryMerchantUserService() {

        QueryWrapper<BusMerchantUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("merchant_id", AuthUserMangerContext.getMerchantId());
        List<BusMerchantUser> busMerchantUsers = busMerchantUserMapper.selectList(queryWrapper);
        return busMerchantUsers.stream().map(item -> {
            String role = item.getType().equals(Integer.valueOf(0)) ? "管理员" : "员工";
            return ResponseMerchantUser.builder()
                    .name(item.getName())
                    .password("******")
                    .phone(item.getPhone())
                    .role(role)
                    .id(item.getId())
                    .build();
        }).collect(Collectors.toList());

    }

    @Override
    public ApiResult deleteMerchantUserService(IdRequest idRequest) {
        BusMerchantUser busMerchantUser = busMerchantUserMapper.selectById(idRequest.getId());
        if (Objects.isNull(busMerchantUser) || !busMerchantUser.getMerchantId().equals(AuthUserMangerContext.getMerchantId())) {
            return ApiResult.fail("数据不存在");
        }
        busMerchantUserMapper.deleteById(idRequest.getId());
        return ApiResult.ok();
    }
}
